//
//  TestThemeViewController.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2021/3/20.
//  Copyright © 2021 moon. All rights reserved.
//

#import "TestThemeViewController.h"
#import "UIButton+TestTheme.h"

@interface TestThemeTableViewCellBase : MKUITableViewCellBase
@end
@implementation TestThemeTableViewCellBase
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.mktheme_backgroundColor = [UIColor mktheme_colorNamed:@"bgColor"];
        self.contentView.layer.mktheme_borderColor = [UIColor mktheme_colorNamed:@"fontColorString"];
        self.contentView.layer.mktheme_borderWidth = mktheme_CGFloatWithName(@"borderWidth");
        self.mktheme_selectionStyle = mktheme_NSIntegerWithName(@"selectionStyle");
    }
    return self;
}
- (CGSize)customSizeThatFits:(CGSize)size{
    return CGSizeMake(size.width,44);
}
@end


@interface TestThemeUILabelCell : TestThemeTableViewCellBase
@property(nonatomic,strong) UILabel *label;
@end
@implementation TestThemeUILabelCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.label = [[UILabel alloc] init];
        self.label.mktheme_text = [NSString mktheme_stringNamed:@"label.text"];
        self.label.mktheme_textColor = [UIColor mktheme_colorNamed:@"labelColor"];
        self.label.mktheme_font = [UIFont mktheme_fontNamed:@"labelFont"];
        [self.contentView addSubview:self.label];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.contentView.bounds;
    self.label.frame = bounds;
}
@end

@interface TestThemeUIButtonCell : TestThemeTableViewCellBase
@property(nonatomic,strong) MKUIFlowLayoutButton *button;
@end
@implementation TestThemeUIButtonCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.button.isAccessibilityElement = YES;
        self.button = [[MKUIFlowLayoutButton alloc] init];
        self.button.imageSize = CGSizeMake(30, 30);
        [self.button mktheme_test_setNormalImage:[UIImage mktheme_imageNamed:@"buttonIcon"] highlightImage:[UIImage mktheme_imageNamed:@"buttonHighlightIconName"]];
        [self.button mktheme_setImage:[UIImage mktheme_imageNamed:@"buttonSelectedIconName"] forState:UIControlStateSelected];
        [self.button mktheme_setTitleColor:[UIColor mktheme_colorNamed:@"labelColor"] forState:UIControlStateNormal];
        [self.button mktheme_setTitle:[NSString mktheme_stringNamed:@"button.text"] forState:UIControlStateNormal];
        [self.button mktheme_setTitle:[NSString mktheme_stringNamed:@"button.text.local"] forState:UIControlStateSelected];
        [self.button mktheme_setTitle:[NSString mktheme_stringNamed:@"button.text.highlight"] forState:UIControlStateHighlighted];
        
        self.button.titleLabel.mktheme_font = [UIFont mktheme_fontNamed:@"labelFont"];
        [self.button addTarget:self action:@selector(_buttonDidTap:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.button];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.contentView.bounds;
    self.button.frame = bounds;
}
- (void)_buttonDidTap:(id)sender{
    self.cellModel[@"selected"] = @(![self.cellModel mk_boolForKeyPath:@"selected" otherwise:NO]);
    [self customReloadCellModel];
}
- (void)customReloadCellModel{
    [super customReloadCellModel];
    self.button.selected = [self.cellModel mk_boolForKeyPath:@"selected" otherwise:NO];
}
@end

@interface TestThemeViewController ()
@property(nonatomic,strong) MKUIFlowLayoutButton *button;
@end

@implementation TestThemeViewController
+ (MKTFuncItem *)funcItem{
    return [MKTFuncItem funcWithName:@"Theme" viewControllerClass:self];
}
- (id)init{
    if(self=[super init]){
        self.tabBarItem.title = self.class.funcItem.name;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = self.tabBarItem.title;
    self.view.mktheme_backgroundColor = [UIColor mktheme_colorNamed:@"bgColor"];
    //
    self.tableView.mktheme_separatorColor = [UIColor mktheme_colorNamed:@"fontColor"];
    //
    MKUIFlowLayoutButton *chgButton = [[MKUIFlowLayoutButton alloc] init];
    [chgButton mktheme_setTitleColor:[UIColor mktheme_colorNamed:@"fontColor"] forState:UIControlStateNormal];
    
    [chgButton setTitle:@"Theme" forState:UIControlStateNormal];
    [chgButton addTarget:self action:@selector(changeTheme) forControlEvents:UIControlEventTouchUpInside];
    [chgButton sizeToFit];
    UIBarButtonItem *chgItem = [[UIBarButtonItem alloc] initWithCustomView:chgButton];
    self.navigationItem.rightBarButtonItem = chgItem;
    
    //
    [self __loadData];
}
- (void)changeTheme{
    NSUInteger index = [MKUIThemeCenter sharedInstance].currentThemeIndex;
    index = (index+1)%([MKUIThemeCenter sharedInstance].themes.count);
    [MKUIThemeCenter sharedInstance].currentThemeIndex = index;
}
- (void)__loadData{
    [self.model removeAllSectionModels];
    {
        MKUITableViewCellModel *cm = [[MKUITableViewCellModel alloc] init];
        cm.cellClass = [TestThemeUILabelCell class];
        [self.model addCellModel:cm];
    }
    {
        MKUITableViewCellModel *cm = [[MKUITableViewCellModel alloc] init];
        cm.cellClass = [TestThemeUIButtonCell class];
        [self.model addCellModel:cm];
    }
    [self.model reloadTableViewData];
}
@end
